package org.shj.spark.datagen

import java.text.SimpleDateFormat
import java.text.DateFormat
import java.util.Date
import java.util.Calendar
import scala.util.Random
import java.io.FileInputStream
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.FileOutputStream


/**
 * 论坛日志数据生成器。
 * 文件格式：
 * date:  yyyy-MM-dd
 * timestamp: 时间戳
 * userId
 * pageId
 * chanel: 具体板块
 * ip: 访问者地址 
 * 
 * create table forum(
 * 	dateStr string, 
 * 	timestampStr string, 
 * 	userId string, 
 * 	pageId string, 
 * 	chanel string, 
 * 	ip string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' lines terminated by '\n';
 * 
 * load data local inpath '/home/shj/forum.log' into table forum;
 */
object Forum {
  val random = new Random()
  val sep = "\t"
  val useTotalCnt = 2000
  val df = new SimpleDateFormat("yyyy-MM-dd")
  
  val channelNames = Array("Spark", "Hadoop", "Kafka", "Scala", "Hive",
				"Impala", "Hbase", "ML", "Flume", "Parquet")
  
  def main(args: Array[String]): Unit = {
    var recordNum = 1000000L
		if(args.length > 0) {
			recordNum = args(0).toLong
		}
    
    val out = new BufferedOutputStream(new FileOutputStream("F:/download/forum.log"))
		
		while(recordNum > 0){
		  out.write(record.getBytes)
		  recordNum -= 1
		}
		
		out.close()
  }
  
  def record = {
    yesterday.concat(sep)
    .concat(radomTimstamp).concat(sep)
    .concat(randomUser).concat(sep)
    .concat(randomPage).concat(sep)
    .concat(randomChannel).concat(sep)
    .concat(randomIp).concat("\n")
  }
  
  def yesterday = {
    val cal = Calendar.getInstance
    cal.add(Calendar.DATE, -1)
    df.format(cal.getTime)
  }
  
  def randomChannel = {
    channelNames(random.nextInt(10))
  }
  
  def radomTimstamp = {
    appendZero(random.nextInt(24)) + ":" + appendZero(random.nextInt(60)) + ":" + appendZero(random.nextInt(60))
  }
  
  def appendZero(num: Int) = {
    if(num < 10){
      "0" + num
    }else{
      num.toString()
    }
  }
  
  def randomUser = {
    "user" + random.nextInt(useTotalCnt)
  }
  
  def randomPage = {
    "page" + random.nextInt(100)
  }
  
  def randomIp = {
    (random.nextInt(2) + 1)  + "" + random.nextInt(2) + "" + random.nextInt(10) + "."  + (random.nextInt(9) + 1)  + "" + random.nextInt(3) + "." + (random.nextInt(2) + 1)  + "" + random.nextInt(3) + "" + random.nextInt(10)
  }
}